package com.flyme.module.base.provider.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysExamItemRemoteService;
import com.flyme.module.base.client.entity.SysConditionitem;
import com.flyme.module.base.client.entity.SysExamItem;
import com.flyme.module.base.client.entity.SysExamination;
import com.flyme.module.base.provider.service.SysConditionitemService;
import com.flyme.module.base.provider.service.SysExamItemService;
import com.flyme.module.base.provider.service.SysExaminationService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 报考条件管理
 * @author zyh
 * @date 2020-7-27
 */
@Api(tags = "报考条件")
@RestController
public class SysExamItemController extends BaseController<SysExamItemService, SysExamItem> implements SysExamItemRemoteService {

    @Autowired
    private SysConditionitemService conditionitemServiceImpl;
    @Autowired
    private SysExaminationService examinationService;

    /**
     * 报考条件列表
     */
    @Override
    public ResultBody list(PagerInfo pagerInfo, SysExamItem examItem) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("*");
        cq.eq("examinationId", examItem.getExaminationId());
        PqGrid pqGrid = bizService.pageList(cq);
        List<EntityMap> lists = (List<EntityMap>) pqGrid.getRows();
        Map<Long, String> itemMap = conditionitemServiceImpl.selectItems();
        for(EntityMap data : lists){
            String itemIds = data.get("itemIds");
            String[] items = itemIds.split(",");
            String itemName = "";
            for(String it : items){
                itemName += itemMap.get(Long.valueOf(it));
            }
            data.put("itemName", itemName);
        }
        pqGrid.setRows(lists);
        return ResultBody.success(pqGrid);
    }

    /**
     * 删除条件
     */
    @Override
    public ResultBody delByTagId(@PathVariable Long id) {
        Boolean tag = bizService.removeById(id);
        if(tag){
            return ResultBody.success("操作成功");
        }else{
            return ResultBody.failed("操作失败");
        }
    }

    /**
     * 添加考试条件
     */
    @Override
    public ResultBody addExamItem(SysExamItem examItem) {
        Boolean tag = bizService.save(examItem);
        return ResultBody.success("添加成功").setData(tag);
    }

    /**
     * 查询考试条件项
     */
    @Override
    public ResultBody selectItem() {
        List<SysConditionitem> list = conditionitemServiceImpl.list();
        EntityMap map = new EntityMap();
        List<SysConditionitem> works = new ArrayList<>();
        List<SysConditionitem> stu = new ArrayList<>();
        for(SysConditionitem item : list){
            if(item.getConditionType().equals(1)){
                stu.add(item);
            }
            if(item.getConditionType().equals(2)){
                works.add(item);
            }
        }
        map.put("stuItemList", stu);
        map.put("workItemList", works);
        return ResultBody.success(map);
    }

    /**
     * 重置申请条件
     */
    @Override
    public ResultBody resetApplyItem(@PathVariable Long examinationId) {
        List<SysExamItem> list = bizService.list(q().eq("examinationId", examinationId));
        String itemString = "";
        if(list.size() > 0){
            for(SysExamItem item : list){
                itemString =  item.getItemIds() + ";" + itemString;
            }
            itemString = itemString.substring(0, itemString.length() - 1);
        }
        examinationService.update(new UpdateWrapper<SysExamination>().set("examItemJson",itemString).eq("id",examinationId));
        return ResultBody.success("操作成功");
    }

}
